﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight;
using System.Windows.Threading;
using GalaSoft.MvvmLight.Messaging;

namespace AzureTracer.ViewModels
{
    public class ProgressIndicatorViewModel : ObservableObject
    {
        DispatcherTimer _timer;

        public ProgressIndicatorViewModel()
        {
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 5);
            _timer.Tick += (sender, e) =>
            {
                _timer.Stop();
                IsVisible = false;                
            };
        }

        /// <summary>
        /// The <see cref="Text" /> property's name.
        /// </summary>
        /// 
        public const string TextPropertyName = "Text";

        private string _text = string.Empty;
        public string Text
        {
            get { return _text; }
            set
            {
                if (_text == value) return;

                _text = value;
                RaisePropertyChanged(TextPropertyName);              
            }
        }

        /// <summary>
        /// The <see cref="IsVisible" /> property's name.
        /// </summary>
        public const string IsVisiblePropertyName = "IsVisible";

        private bool _isVisible = false;
        public bool IsVisible
        {
            get { return _isVisible; }
            set
            {
                if (_isVisible == value) return;

                _isVisible = value;
                RaisePropertyChanged(IsVisiblePropertyName);
            }
        }

     
        private bool _isLoading = false;
        public bool IsLoading
        {
            get { return _isLoading; }
            set
            {
                if (_isLoading == value) return;

                _isLoading = value;
                RaisePropertyChanged("IsLoading");
                RaisePropertyChanged("IsNotLoading");
            }
        }

        public bool IsNotLoading { get { return !IsLoading; } }

        public void StartLoading(string message)
        {
            Text = message;
            IsLoading = true;
            IsVisible = true;
        }

        public void EndLoading()
        {
            IsVisible = false;
            IsLoading = false;
        }

        public void ShowMessage(string message)
        {
            Text = message;
            IsLoading = false;
            IsVisible = true;

            if (_timer.IsEnabled)
                _timer.Stop();
            _timer.Start();
        }

       
    }
}
